<?php
include "connect.php";
header('content-type:text/html;charset=utf-8');
//接收信息
$userName = $_POST['userName'];
$password = $_POST['password'];
$password1 = $_POST['password1'];
$gender = $_POST['gender'];
$education = $_POST['education'];
$habit = $_POST['habit'];
$profile = $_POST['profile'];

$isInfoCanUse = false;

//1.判断下用户首字母是否符合规范
$char = $userName{0};
$ascii = ord($char);
$targetFile = '';
if (($ascii >= 97 && $ascii <= 122) || ($ascii >= 65 && $ascii <= 90)) {
    $isInfoCanUse = true;
    //2.判断用户名长度是否符合规范
    $userLen = strlen($userName);
    if ($userLen >= 6 && $userLen <= 18) {
        //3.判断密码是否为空
        $pwdLen = strlen($password);
        if ($pwdLen > 0) {
            //4.判断两次密码是否一致
            if ($password === $password1) {
                //处理上传的图片
                if (isset($_FILES['photo'])) {
                    $errors = array();
                    $file_name = $_FILES['photo']['name'];
                    $file_size = $_FILES['photo']['size'];
                    $file_tmp = $_FILES['photo']['tmp_name'];
                    $file_type = $_FILES['photo']['type'];
                    $file_ext = strtolower(end(explode('.', $_FILES['photo']['name'])));

                    $extensions = array("jpeg", "jpg", "png");

                    if (in_array($file_ext, $extensions) === false) {
                        $errors[] = "extension not allowed, please choose a JPEG or PNG file.";
                    }

                    if ($file_size > 5000000) {
                        $errors[] = 'File size must be excately 5 MB';
                    }

                    if (empty($errors) == true) {
                        $targetDir = "upload/"; //指定保存图片的目录
                        $targetFile = $targetDir . time() . '.' . $file_ext; // 使用时间戳作为文件名
                        move_uploaded_file($file_tmp, $targetFile);
                        echo "文件 " . basename($_FILES["photo"]["name"]) . " 已成功上传.";
                    } else {
                        print_r($errors);
                    }
                }

                //把用户的注册的信息写入数据库中
                //数据库连接
                $dbServername = "localhost";
                $dbuserName = "root";
                $dbPassword = "123456";
                $dbName = "business";

                // 创建连接
                $conn = mysqli_connect($dbServername, $dbuserName, $dbPassword, $dbName);

                // 检测连接
                if (!$conn) {
                    die("连接失败: " . $conn->connect_error);
                } else {
                    echo "连接成功";
                }

                //检测用户名是否被占用
                $sql = "SELECT userName FROM users WHERE userName='$userName'";
                $result = mysqli_query($conn, $sql);//找不到也是执行成功，返回结果集
                $test = mysqli_fetch_assoc($result);
                if ($test != false)//记录不为空就是找到了
                {
                    $userNameErr = "该用户名已存在，请重新注册";
                    echo $userNameErr;
                    $isInfoCanUse = false;
                }

                $isRegSuccess = false;//检测是否注册成功
                $avtar = '/' . $targetFile;
                if ($isInfoCanUse === true) {//如果信息填写完整，就注册
                    $sql = "INSERT INTO users(userName,password,gender,educational,habit,profile,avtar) VALUES('$userName','$password','$gender','$education','$habit','$profile','$avtar')";
                    if (mysqli_query($conn, $sql)) {
                        $isRegSuccess = true;
                    } else {
                        $isRegSuccess = false;
                    }
                    echo '恭喜您,注册成功<br>';
                    echo '<br>3秒钟之后跳转到主页页面<br>';
                    echo '<meta http-equiv="refresh" content="3;url=index.php">';
                } else {
                    echo '两次密码不一致';
                    $isInfoCanUse = false;
                }
            } else {
                echo '密码不能为空';
                $isInfoCanUse = false;
            }
        } else {
            echo "{$userName}长度不符合规范<br>";
            $isInfoCanUse = false;
        }
    } else {
        echo "{$userName}用户名不是以字母开始";
        $isInfoCanUse = false;
    }
}
?>
